package com.guiji.quartz.domain;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.guiji.quartz.config.ProxyConfig;
import com.guiji.quartz.task.DataSaveUtil;
import com.guiji.quartz.util.DateFormatUtil;
import com.guiji.quartz.util.DelPicUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @date 2021/11/29 16:37
 */
public class Fxstreet {
	private static final Logger log = LoggerFactory.getLogger(Fxstreet.class);

	public static void mainMethod() throws Exception {
		HttpResponse response = null;
		try {
			response = HttpRequest.post("https://50dev6p9k0-3.algolianet.com/1/indexes/*/queries?x-algolia-agent=Algolia%20for%20vanilla%20JavaScript%20(lite)%203.25.1%3Binstantsearch.js%202.6.3%3BJS%20Helper%202.24.0&x-algolia-application-id=50DEV6P9K0&x-algolia-api-key=cd2dd138c8d64f40f6d06a60508312b0")
				.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
				.header("Accept-Encoding", "gzip, deflate")
				.body("{\"requests\":[{\"indexName\":\"FxsIndexPro\",\"params\":\"query=&hitsPerPage=17&maxValuesPerFacet=9999&page=0&filters=CultureName%3Aen%20AND%20(Category%3A'News'%20OR%20Category%3A'Breaking%20News'%20OR%20Category%3A'Premium%20News')&facets=%5B%22Tags%22%5D&tagFilters=\"}]}")
				.setHttpProxy(ProxyConfig.url, ProxyConfig.ip)
				.setConnectionTimeout(30000)
				.execute();
		} catch (Exception e) {
			log.error("代理失败1-Fxstreet" + e);
			return;
		}
		String resp = response.body();
		Set<String> resUrl = getUrl(resp);
		getContent(resUrl);

	}

	private static void getContent(Set<String> listContent) {
		for (String url : listContent) {
			log.info("getContent执行到-{},listContent长度-{}", url, listContent.size());
			String doc;
			JXDocument jxDocument;
			try {
				HttpResponse response = HttpRequest.get(url).setHttpProxy(ProxyConfig.url, ProxyConfig.ip).setConnectionTimeout(30000).execute();
				doc = response.body();
				jxDocument = new JXDocument(doc);
			} catch (Exception e) {
				log.info("代理失败2" + e);
				continue;
			}
			String title = null;
			try {
				title = (String) jxDocument.sel("//h1//text()").get(0);
			} catch (Exception e) {
				log.error("标题错误-------------" + url + "-------------------标题错误");
				continue;
			}
			Document document = null;
			String content = null;
			try {
				document = Jsoup.parse(doc);
				content = document.getElementsByAttributeValue("id","fxs_article_content").get(0).toString();
				content = content.replaceAll("</p>|</h2>|</h3>", "\\$\\$\\$").replaceAll("\n","")
					.replaceAll("<.*?>", " ").replaceAll("&nbsp;"," ");
			} catch (Exception e) {
				continue;
			}

			List<String> newImgs = new ArrayList<>();

			Date date = new Date();

			if (content.length() > 50000 || content.replaceAll("\\$|[\\s\\p{Zs}]","").length() < 20 || newImgs.size() > 20) {
				continue;
			}
			try {
				int i = RandomUtil.randomInt(1, 4);
				DataSaveUtil.saveData(title, content, newImgs, url, date, "https://www.fxstreet.com/news", "Fxstreet-Forex-"+i);
				log.info(Thread.currentThread().getName() + "-----------------Fxstreet-Forex----------------");
			} catch (Exception e) {
				continue;
			}

		}
	}

	private static Set<String> getUrl(String content) {

		Set<String> res = new HashSet<>();
		String pattern = "\"FullUrl\":\".*?\"";
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(content);
		while (m.find()) {
			res.add(m.group().replaceAll("\"FullUrl\":\"|\"",""));
		}
		return res;
	}

//	public static void main(String[] args) throws Exception {
//		mainMethod();
//	}
}
